package com.kn.musicmanagement.service.Impl;


import com.kn.musicmanagement.controller.Result;
import com.kn.musicmanagement.mapper.SingerMapper;
import com.kn.musicmanagement.mapper.SysMessMapper;
import com.kn.musicmanagement.pojo.Singer;
import com.kn.musicmanagement.pojo.SysMess;
import com.kn.musicmanagement.pojo.User;
import com.kn.musicmanagement.pojo.singerEx;
import com.kn.musicmanagement.service.EmailSendService;
import com.kn.musicmanagement.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.List;

import static com.kn.musicmanagement.Utils.messConstants.TITLE_APPROVED_MESS;
import static com.kn.musicmanagement.Utils.songUtil.*;

@Service
public class SingerServiceImpl implements SingerService {

    @Autowired
    private SingerMapper singerMapper;

    @Autowired
    private SysMessMapper sysMessMapper;

    @Autowired
    private EmailSendService emailSendService;

    @Override
    public List<Singer> getAllSinger() {
        List<Singer> singers = singerMapper.getAllSinger();
        return singers;
    }

    @Override
    public Result getUnapprovedSinger() {
        Result result = new Result();
        List<singerEx> unapprovedSinger = singerMapper.getUnapprovedSinger();
        if (!unapprovedSinger.isEmpty()) {
            result.setCode(200);
            result.setData(unapprovedSinger);
        } else {
            result.setCode(500);
        }
        return result;
    }

    @Override
    public Result getSingerDe(String singer_ID) {
        Result result = new Result();
        Singer singerDe = singerMapper.getSingerDe(singer_ID);
        System.out.println(singerDe);
        result.setCode(200);
        result.setData(singerDe);
        return result;
    }

    @Override
    public Result ApprovedSinger(String singer_ID, String singer_Name) throws MessagingException {
        int flag = singerMapper.ApprovedSinger(singer_ID);
        //系统消息内通知用户
        User user = singerMapper.getEmailBySingerID(singer_ID);
        String mess = createApprovedSingerMess(user.getUser_Name(), singer_Name);
        SysMess sysMess = new SysMess();
        sysMess.setMess_text(mess);
        sysMess.setMess_title(TITLE_APPROVED_MESS);
        sysMess.setUser_ID(user.getUser_ID());
        sysMessMapper.postMess(sysMess);
        //发送邮箱通知
        emailSendService.sendApprovedSinger(user, singer_Name);
        Result result = new Result();
        if (flag > 0) {
            result.setCode(200);
        }
        return result;
    }

    @Override
    public Result NoExamineSinger(String singer_ID, String singer_Name, String examination) throws MessagingException {
        int flag = singerMapper.NoExamineSinger(singer_ID);
        //系统消息内通知用户
        User user = singerMapper.getEmailBySingerID(singer_ID);
        String NoExamineSingerMess = createNoExamineSingerMess(user.getUser_Name(), singer_Name, examination);
        SysMess sysMess = new SysMess();
        sysMess.setMess_text(NoExamineSingerMess);
        sysMess.setMess_title(TITLE_APPROVED_MESS);
        sysMess.setUser_ID(user.getUser_ID());
        sysMessMapper.postMess(sysMess);
        //发送邮箱通知
        emailSendService.sendNoExamineSingerMess(user, singer_Name, examination);
        Result result = new Result();
        result.setCode(200);
        return result;
    }
}
